package JUC;

import java.util.concurrent.*;

public class CompletableFutureUseDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService=Executors.newFixedThreadPool(3);
        //Future1();
        CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName());
            try {
                TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {throw new RuntimeException(e);}
            return ThreadLocalRandom.current().nextInt();
        },executorService).whenComplete((v,e)->{
            if (e==null){
                System.out.println("计算完成,系统更新value:"+v);
            }
        }).exceptionally(e->{
            e.printStackTrace();
            System.out.println("异常了");
            return null;
        });
        executorService.shutdown();
        System.out.println(Thread.currentThread().getName()+"先去忙了");

    }
    static void Future1() throws ExecutionException, InterruptedException {
        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName());
            try {
                TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {throw new RuntimeException(e);}
            return ThreadLocalRandom.current().nextInt();
        });

        System.out.println(Thread.currentThread().getName()+"先去忙了");
        System.out.println(integerCompletableFuture.get());
    }
}
